package com.example.mapper;

import com.example.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {
    //新增文章分类
       // 查找最小空缺ID
    @Select("SELECT MIN(t1.id + 1) FROM category t1 " +
            "LEFT JOIN category t2 ON t1.id + 1 = t2.id " +
            "WHERE t2.id IS NULL AND t1.id < 99")
    Integer findFirstGapId();
    // 查找当前最大ID
    @Select("SELECT COALESCE(MAX(id), 0) FROM category")
    Integer findMaxId();

    @Insert("insert into category(id,category_name,category_alias,create_user,create_time,update_time)" +
            "values(#{id},#{categoryName},#{categoryAlias},#{createUser},#{createTime},#{updateTime})")
    void add(Category category);

    //查询分类列表
    @Select("select * from category where create_user = #{userId}")
    List<Category> list(Integer userId);

    //根据id查看分类详情
    @Select("select * from category where id = #{id}")
    Category findById(Integer id);

    //更新分类
    @Update("update category set category_name=#{categoryName},category_alias=#{categoryAlias},update_time=#{updateTime}" +
            "where id=#{id}")
    void update(Category category);

    //删除分类
    @Delete("DELETE FROM category WHERE id = #{id}")
    void delete(Integer id);
}
